#include <stdio.h>
#include <unistd.h>
#include <cassert>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    //创建管道
    int pipefd[2];
    int ret = pipe(pipefd);
    assert(ret == 0);

    //创建子进程
    pid_t id = fork();
    int cnt = 1;
    if(id == 0)
    {
        //子进程
        //关闭写端
        close(pipefd[0]);
        close(pipefd[1]);
        char buff[1024] = {0};
        ssize_t num = read(pipefd[0], buff, sizeof(buff));
        if(num == 0)
        {
            cout << "写端关闭\n"<< endl;
        }
        // assert(num != -1);
        printf("%s\n", buff);
    }
    //父进程关闭读端
    close(pipefd[0]);
    close(pipefd[1]);
    const char* str = "i am father";
    ssize_t num = write(pipefd[1], str, strlen(str));
    return 0;
}